//
//  LSZAudioSearchVC.m
//  JKFuYinProject
//
//  Created by 九酷 on 2018/4/13.
//  Copyright © 2018年 LC. All rights reserved.
//

#import "LSZAudioSearchVC.h"

@interface LSZAudioSearchVC ()

@end

@implementation LSZAudioSearchVC
-(void)viewWillAppear:(BOOL)animated
{
    [LSZRequestResolutionDic initWithSearchKey:^(BOOL isOk, NSString * str, LSZSearchKeyManager * manager) {
        if (isOk == YES) {
            self.SearchKeyMg = manager;
            [self.collectionView reloadData];
        }
    }];
}
#pragma mark - 跳转到搜索内容界面
-(void)initWithDataCountStr:(NSString *)countStr :(NSInteger)countPage
{
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
    LSZParentClassSearchVC * VC = [[LSZParentClassSearchVC alloc]init];
    VC.searchKeyword = [NSString stringWithFormat:@"%@",countStr];
    VC.fromWhere = self.fromWhere;
    [self.navigationController pushViewController:VC animated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.historyMutArray = [[LCMusicDataBase sharedDataBase] getAllPerson];
    self.historyMutArray = (NSMutableArray *)[[self.historyMutArray reverseObjectEnumerator] allObjects];

    [self.collectionView registerClass:[LSZSearchCollectionViewCell class] forCellWithReuseIdentifier:@"LSZSearchCollectionViewCell"];
    [self.tableViewHistorical registerClass:[LSZSearchHistoricalTableViewCell class] forCellReuseIdentifier:@"LSZSearchHistoricalTableViewCell"];
    self.mutArrayContent = [[NSMutableArray alloc]init];
}
-(UITableView *)tableViewHistorical
{
    if (_tableViewHistorical == nil) {
        _tableViewHistorical = [[UITableView alloc]initWithFrame:CGRectMake(0 , CGRectGetMaxY(_collectionView.frame), ScreenWidth, ScreenHeight-CGRectGetMaxY(_collectionView.frame)) style:UITableViewStylePlain];
        _tableViewHistorical.delegate = self;
        _tableViewHistorical.dataSource = self;
        _tableViewHistorical.estimatedRowHeight =0;
        _tableViewHistorical.estimatedSectionFooterHeight =0;
        _tableViewHistorical.estimatedSectionHeaderHeight =0;
        _tableViewHistorical.separatorInset = UIEdgeInsetsMake(0, 20, 0, 20);
        _tableViewHistorical.separatorColor = divider;
        _tableViewHistorical.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
        [self.view addSubview:_tableViewHistorical];
    }
    return _tableViewHistorical;
}
-(UICollectionView *)collectionView
{
    if (_collectionView == nil) {
        EqualSpaceFlowLayout *flowLayout = [[EqualSpaceFlowLayout alloc] init];
        flowLayout.delegate = self;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) collectionViewLayout:flowLayout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        // 是否显示垂直方向指示标, 继承于UIScrollView, 他的方法可以调用
        _collectionView.showsVerticalScrollIndicator = NO;
        [self.view addSubview:_collectionView];
    }
    return _collectionView;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    LSZSearchCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LSZSearchCollectionViewCell" forIndexPath:indexPath];

    LSZSearchKeyMg * mg = nil;
    mg = self.SearchKeyMg.mutMgArray[indexPath.row];
    cell.labelTitle.text = [NSString stringWithFormat:@"%@",mg.title];
    NSString * strTitle = [NSString stringWithFormat:@"%@",mg.title];
    CGFloat widthHeight;
    if (ScreenWidth == 320) {
        //5
        widthHeight = [LSZSpecialPackaging caculateWidthOfText:1000 height:25 font:13.5 :strTitle];
    }else if(ScreenWidth == 375){
        // 6 7
        widthHeight = [LSZSpecialPackaging caculateWidthOfText:1000 height:29 font:14 :strTitle];
    }else {
        //plus
        widthHeight = [LSZSpecialPackaging caculateWidthOfText:1000 height:33 font:14.5 :strTitle];

    }
    CGRect rect = cell.labelTitle.frame;
    rect.size.width = widthHeight;
    cell.labelTitle.frame = rect;
    if (indexPath.row == self.SearchKeyMg.mutMgArray.count-1) {
        if (iPhoneX) {
            self.collectionView.frame = CGRectMake(0, 0, ScreenWidth, CGRectGetMaxY(cell.frame)+cell.frame.size.height+80);
        }else{
            self.collectionView.frame = CGRectMake(0, 0, ScreenWidth, CGRectGetMaxY(cell.frame)+cell.frame.size.height+50);
        }
        self.tableViewHistorical.frame = CGRectMake(0 , CGRectGetMaxY(_collectionView.frame), ScreenWidth, ScreenHeight-CGRectGetMaxY(_collectionView.frame));
    }
    return cell;
}
#pragma mark - 设置每个单元格的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LSZSearchKeyMg * mg = nil;
    mg = self.SearchKeyMg.mutMgArray[indexPath.row];
    NSString * strTitle = [NSString stringWithFormat:@"%@",mg.title];
    CGFloat widthHeight;
    if (ScreenWidth == 320) {
        //5
        widthHeight = [LSZSpecialPackaging caculateWidthOfText:1000 height:25 font:13.5 :strTitle];
        return CGSizeMake(widthHeight,25);
    }else if(ScreenWidth == 375){
        // 6 7
        widthHeight = [LSZSpecialPackaging caculateWidthOfText:1000 height:29 font:14 :strTitle];
        return CGSizeMake(widthHeight,29);
    }else {
        //plus
        widthHeight = [LSZSpecialPackaging caculateWidthOfText:1000 height:33 font:14.5 :strTitle];
        return CGSizeMake(widthHeight,33);
    }
}
//设置单元格间的横向间距
- (CGFloat) collectionView:(UICollectionView *)collectionView
                    layout:(UICollectionViewLayout *)collectionViewLayout
minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 10.0f;
}
// 设置每个分区返回多少item
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return  self.SearchKeyMg.mutMgArray.count;
}
// 设置集合视图有多少个分区
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
// 点击item触发的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    self.countPage = 1;
    LSZSearchKeyMg * mg = nil;
    mg = self.SearchKeyMg.mutMgArray[indexPath.row];
    self.searchString = mg.title;
    self.textFieldSearch.text = self.searchString;
    [self searchCount:self.searchString :self.countPage];
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
}
-(void)searchCount :(NSString *)countStr  :(NSInteger)countPage
{
    //加入历史记录
    NSString *name = countStr;
    Person *person = [[Person alloc] init];
    person.name = name;
    [[LCMusicDataBase sharedDataBase] addPerson:person];
    self.historyMutArray = [[LCMusicDataBase sharedDataBase] getAllPerson];
    self.historyMutArray = (NSMutableArray *)[[self.historyMutArray reverseObjectEnumerator] allObjects];
    [self.tableViewHistorical reloadData];
    [self initWithDataCountStr:countStr :countPage];
}
@end
